<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
        <h2>项 目 组 价 格 模 板 新 增</h2>
        <el-button
          type="success"
          plain
          size="small"
          icon="fa fa-mail-forward"
          @click="returnList"
        >
          返 回</el-button
        >
      </div>
      <el-form
        :model="addForm"
        ref="addForm"
        label-width="200px"
        :rules="roleRules"
      >
        <el-row>
          <el-form-item v-if="isAdd" label="模板名称" prop="modelName">
            <el-input
              v-model="addForm.modelName"
              clearable
              disabled
            ></el-input>
          </el-form-item>

          <el-form-item v-if="isAdd" label="关联康来项目组" prop="itemId">
            <el-select
              v-model="addForm.itemId"
              filterable
              placeholder="请选择项目"
              :loading="loadingIns"
            >
              <el-option
                v-for="item in pickReportItemIdList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item v-if="isUpdate" label="项目名称" prop="itemName">
            <el-input v-model="addForm.itemName" clearable disabled></el-input>
          </el-form-item>

          <el-form-item label="原价" prop="itemPrice">
            <el-input v-model="addForm.itemPrice" clearable></el-input>
          </el-form-item>
          <el-form-item label="合作价" prop="itemSalePrice">
            <el-input v-model="addForm.itemSalePrice"></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <el-button
              type="primary"
              @click="sureClick(0)"
              :disabled="sureDisabled"
              >提 交</el-button
            >
            <el-button
              type="primary"
              @click="sureClick(1)"
              :disabled="sureDisabled"
              >提交并刷新</el-button
            >
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import PriceModelApi from "@/api/pricemodel/pricemodel";
import ReportItemApi from "@/api/report/reportItem";
export default {
  data() {
    return {
      addForm: {
        //  项目组价格模板ID
        itemPriceModelId: "",
        //	康来项目组ID
        itemId: "",
        //项目名称
        itemName: "",
        //	原价格
        itemPrice: "",
        //合作价格
        itemSalePrice: "",

        itemPriceId: "",
        modelName: "",
        modelType: "",
        modelDesc: "",
        remark: ""
      },
      loadingIns: false,
      //修改
      isUpdate: false,
      //新增
      isAdd: false,
      pickReportItemIdList: [], // 报告单项目列表
      modelTypeList: [
        { label: "医院", value: 1 },
        { label: "医检", value: 2 }
      ],
      sureDisabled: false,
      roleRules: {
        modelName: [
          {
            required: true,
            message: "请输入模板名称",
            trigger: "blur"
          }
        ],
        modelType: [
          {
            required: true,
            message: "请选择模板类型",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      //新增
      if (this.$route.query.itemPriceModelId) {
        this.isUpdate = false;
        this.isAdd = true;
        this.addForm.itemPriceModelId = this.$route.query.itemPriceModelId;

        this.listReportItem();
      }
      //修改
      if (this.$route.query.itemPriceId) {
        this.isUpdate = true;
        this.isAdd = false;
        this.addForm.itemPriceId = this.$route.query.itemPriceId;

        this.getData();
      }
    },
    listReportItem() {
      // 获取报告单项目组列表
      let _this = this;
      _this.loadingIns = true;
      _this.addForm.modelName = this.$route.query.itemPriceModelName
      ReportItemApi.getReportItemsNameList({query:1}).then(res => {
        // console.log(res.data);
        setTimeout(() => {
          _this.loadingIns = false;
          _this.pickReportItemIdList = res.data.map(item => {
            return {
              value: item.itemId,
              label: item.itemName
            };
          });
        }, 200);
      });
    },
    getData() {
      let _this = this;
      let params = {
        id: _this.addForm.itemPriceId
      };
      PriceModelApi.itemPrice(params).then(res => {
        // console.log(res.data,180)
        this.$Utils.bindingBean(this.addForm, res.data);
        if (res.data.modelType) {
          this.dataForm.modelType = parseInt(res.data.modelType);
        }
      });
    },
    sureClick(state) {
      let _this = this;
      _this.$refs.addForm.validate(valid => {
        if (valid) {
          if (this.addForm.itemPriceId) {
            //做一个基础的判断,itemPriceId是修改
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ALTER,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.editData(state);
            });
          } else {
            this.$confirm(
              message.MSG_CONFIRM_SAVE_ADD,
              message.TITLE_PROMPT,
              {}
            ).then(() => {
              this.addData();
            });
          }
        }
      });
    },
    addData() {
      let _this = this;
      let cond = {
        itemPriceModelId: _this.addForm.itemPriceModelId,
        itemId: _this.addForm.itemId,
        itemPrice: _this.addForm.itemPrice,
        itemSalePrice: _this.addForm.itemSalePrice
      };
      PriceModelApi.addDatac(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      });
    },
    //修改
    editData(state) {
      let _this = this;
      var params = {};
      if (state == 1) {
        params = {
          itemPriceId: _this.addForm.itemPriceId,
          itemName: _this.addForm.itemName,
          itemPrice: _this.addForm.itemPrice,
          itemSalePrice: _this.addForm.itemSalePrice,
          update: 1
        };
      } else {
        params = {
          itemPriceId: _this.addForm.itemPriceId,
          itemName: _this.addForm.itemName,
          itemPrice: _this.addForm.itemPrice,
          itemSalePrice: _this.addForm.itemSalePrice
        };
      }
      PriceModelApi.updateDatac(params).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      });
    },
    returnList() {
      // this.$router.go(-1)
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/item/priceModelsublList" });
    }
  }
};
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
<style scoped>
.add_wrap {
  padding-top: 10px;
  padding-left: 23px;
}
</style>
